#include <QtGui>

#include "cuboidDialog.h"
#include "src/vector3d.h"

CuboidDialog::CuboidDialog(int _id, QWidget *_parent)
    : QDialog(_parent), id(_id){

    setWindowTitle(QString::fromUtf8("Novo cubóide"));

    cuboid = NULL;
    ambientR = ambientG = ambientB = diffuseR = diffuseG = diffuseB = specularR = specularG = specularB = 200;
    minX = minY = minZ = 0.0;
    maxX = maxY = maxZ = 1.0;

    createLayout();
}

CuboidDialog::CuboidDialog(CuboidPrimitive *_cuboid, ObjectTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), cuboid(_cuboid), item(_item){

    setWindowTitle(QString::fromUtf8("Modificar objeto ").append(QString::number(_cuboid->getId())));

    const Material &material = cuboid->getMaterial();
    const double *color = material.getAmbient();
    ambientR = color[0] * 255;
    ambientG = color[1] * 255;
    ambientB = color[2] * 255;

    color = material.getDiffuse();
    diffuseR = color[0] * 255;
    diffuseG = color[1] * 255;
    diffuseB = color[2] * 255;

    color = material.getSpecular();
    specularR = color[0] * 255;
    specularG = color[1] * 255;
    specularB = color[2] * 255;

    Vector3D min = cuboid->getMin();
    minX = min.x;
    minY = min.y;
    minZ = min.z;

    Vector3D max = cuboid->getMax();
    maxX = max.x;
    maxY = max.y;
    maxZ = max.z;

    createLayout();
}

CuboidDialog::~CuboidDialog(){
    delete ambientPushButton;
    delete diffusePushButton;
    delete specularPushButton;
//    delete minXDoubleSpinBox;
//    delete minYDoubleSpinBox;
//    delete minZDoubleSpinBox;
//    delete maxXDoubleSpinBox;
//    delete maxYDoubleSpinBox;
//    delete maxZDoubleSpinBox;
}

void CuboidDialog::createLayout(){
//    QLabel *minXLabel = new QLabel(tr("X:"));
//
//    minXDoubleSpinBox = new QDoubleSpinBox();
//    minXDoubleSpinBox->setRange(-50.0, 50.0);
//    minXDoubleSpinBox->setSingleStep(1.0);
//    minXDoubleSpinBox->setValue(minX);
//    connect(minXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMinX(double)));
//
//
//    QLabel *minYLabel = new QLabel(tr("Y:"));
//
//    minYDoubleSpinBox = new QDoubleSpinBox();
//    minYDoubleSpinBox->setRange(-50.0, 50.0);
//    minYDoubleSpinBox->setSingleStep(1.0);
//    minYDoubleSpinBox->setValue(minY);
//    connect(minYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMinY(double)));
//
//
//    QLabel *minZLabel = new QLabel(tr("Z:"));
//
//    minZDoubleSpinBox = new QDoubleSpinBox();
//    minZDoubleSpinBox->setRange(-50.0, 50.0);
//    minZDoubleSpinBox->setSingleStep(1.0);
//    minZDoubleSpinBox->setValue(minZ);
//    connect(minZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMinZ(double)));
//
//
//    QHBoxLayout *minLayout = new QHBoxLayout;
//    minLayout->addWidget(minXLabel);
//    minLayout->addWidget(minXDoubleSpinBox);
//    minLayout->addStretch(1);
//    minLayout->addWidget(minYLabel);
//    minLayout->addWidget(minYDoubleSpinBox);
//    minLayout->addStretch(1);
//    minLayout->addWidget(minZLabel);
//    minLayout->addWidget(minZDoubleSpinBox);
//    minLayout->addStretch(1);
//
//    QGroupBox *minGroupBox = new QGroupBox(QString::fromUtf8("Ponto mínimo"));
//    minGroupBox->setLayout(minLayout);
//
//
//    QLabel *maxXLabel = new QLabel(tr("X:"));
//
//    maxXDoubleSpinBox = new QDoubleSpinBox();
//    maxXDoubleSpinBox->setRange(-50.0, 50.0);
//    maxXDoubleSpinBox->setSingleStep(1.0);
//    maxXDoubleSpinBox->setValue(maxX);
//    connect(maxXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMaxX(double)));
//
//
//    QLabel *maxYLabel = new QLabel(tr("Y:"));
//
//    maxYDoubleSpinBox = new QDoubleSpinBox();
//    maxYDoubleSpinBox->setRange(-50.0, 50.0);
//    maxYDoubleSpinBox->setSingleStep(1.0);
//    maxYDoubleSpinBox->setValue(maxY);
//    connect(maxYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMaxY(double)));
//
//
//    QLabel *maxZLabel = new QLabel(tr("Z:"));
//
//    maxZDoubleSpinBox = new QDoubleSpinBox();
//    maxZDoubleSpinBox->setRange(-50.0, 50.0);
//    maxZDoubleSpinBox->setSingleStep(1.0);
//    maxZDoubleSpinBox->setValue(maxZ);
//    connect(maxZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeMaxZ(double)));
//
//
//    QHBoxLayout *maxLayout = new QHBoxLayout;
//    maxLayout->addWidget(maxXLabel);
//    maxLayout->addWidget(maxXDoubleSpinBox);
//    maxLayout->addStretch(1);
//    maxLayout->addWidget(maxYLabel);
//    maxLayout->addWidget(maxYDoubleSpinBox);
//    maxLayout->addStretch(1);
//    maxLayout->addWidget(maxZLabel);
//    maxLayout->addWidget(maxZDoubleSpinBox);
//    maxLayout->addStretch(1);
//
//    QGroupBox *maxGroupBox = new QGroupBox(QString::fromUtf8("Ponto máximo"));
//    maxGroupBox->setLayout(maxLayout);


    ambientPushButton = new QPushButton(tr("Cor ambiente"));
    connect(ambientPushButton, SIGNAL(clicked()), this, SLOT(changeAmbient()));

    diffusePushButton = new QPushButton(tr("Cor difusa"));
    connect(diffusePushButton, SIGNAL(clicked()), this, SLOT(changeDiffuse()));

    specularPushButton = new QPushButton(tr("Cor especular"));
    connect(specularPushButton, SIGNAL(clicked()), this, SLOT(changeSpecular()));
    setButtonColor();


    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
//    mainLayout->addWidget(minGroupBox);
//    mainLayout->addWidget(maxGroupBox);
    mainLayout->addWidget(ambientPushButton);
    mainLayout->addWidget(diffusePushButton);
    mainLayout->addWidget(specularPushButton);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void CuboidDialog::setButtonColor(){
    QString ambientStr;
    if(ambientR + ambientB + ambientG < 400){
        ambientStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        ambientStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    ambientStr.append(QString::fromUtf8("background-color: rgb("));
    ambientStr.append(QString::number(ambientR));
    ambientStr.append(QString::fromUtf8(", "));
    ambientStr.append(QString::number(ambientG));
    ambientStr.append(QString::fromUtf8(", "));
    ambientStr.append(QString::number(ambientB));
    ambientStr.append(QString::fromUtf8(");"));

    ambientPushButton->setStyleSheet(ambientStr);



    QString diffuseStr;
    if(diffuseR + diffuseB + diffuseG < 400){
        diffuseStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        diffuseStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    diffuseStr.append(QString::fromUtf8("background-color: rgb("));
    diffuseStr.append(QString::number(diffuseR));
    diffuseStr.append(QString::fromUtf8(", "));
    diffuseStr.append(QString::number(diffuseG));
    diffuseStr.append(QString::fromUtf8(", "));
    diffuseStr.append(QString::number(diffuseB));
    diffuseStr.append(QString::fromUtf8(");"));

    diffusePushButton->setStyleSheet(diffuseStr);



    QString specularStr;
    if(specularR + specularB + specularG < 400){
        specularStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        specularStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    specularStr.append(QString::fromUtf8("background-color: rgb("));
    specularStr.append(QString::number(specularR));
    specularStr.append(QString::fromUtf8(", "));
    specularStr.append(QString::number(specularG));
    specularStr.append(QString::fromUtf8(", "));
    specularStr.append(QString::number(specularB));
    specularStr.append(QString::fromUtf8(");"));

    specularPushButton->setStyleSheet(specularStr);
}

void CuboidDialog::acceptAndCreate(){
    if(!cuboid){
        cuboid = new CuboidPrimitive(Vector3D(minX, minY, minZ), Vector3D(maxX, maxY, maxZ), Material(ambientR/255.0, ambientG/255.0, ambientB/255.0, diffuseR/255.0, diffuseG/255.0, diffuseB/255.0, specularR/255.0, specularG/255.0, specularB/255.0), id);

        done(cuboid);
    }else{
        cuboid->setMaterial(Material(ambientR/255.0, ambientG/255.0, ambientB/255.0, diffuseR/255.0, diffuseG/255.0, diffuseB/255.0, specularR/255.0, specularG/255.0, specularB/255.0));
        cuboid->setMinMax(Vector3D(minX, minY, minZ), Vector3D(maxX, maxY, maxZ));
    }

    accept();
    done();
}

void CuboidDialog::changeAmbient(){
    QColor color = QColorDialog::getColor(QColor(ambientR, ambientG, ambientB), this, "Escolha a cor ambiente");
    if (color.isValid()){
        ambientR = color.red();
        ambientG = color.green();
        ambientB = color.blue();
    }
    setButtonColor();
}

void CuboidDialog::changeDiffuse(){
    QColor color = QColorDialog::getColor(QColor(diffuseR, diffuseG, diffuseB), this, "Escolha a cor difusa");
    if (color.isValid()){
        diffuseR = color.red();
        diffuseG = color.green();
        diffuseB = color.blue();
    }
    setButtonColor();
}

void CuboidDialog::changeSpecular(){
    QColor color = QColorDialog::getColor(QColor(specularR, specularG, specularB), this, "Escolha a cor especular");
    if (color.isValid()){
        specularR = color.red();
        specularG = color.green();
        specularB = color.blue();
    }
    setButtonColor();
}
//
//void CuboidDialog::changeMinX(double _x){
//    minX = _x;
//    if(minX >= maxX){
//        maxX = minX + 1;
//        maxXDoubleSpinBox->setValue(maxX);
//    }
//}
//
//void CuboidDialog::changeMinY(double _y){
//    minY = _y;
//    if(minY >= maxY){
//        maxY = minY + 1;
//        maxYDoubleSpinBox->setValue(maxY);
//    }
//}
//
//void CuboidDialog::changeMinZ(double _z){
//    minZ = _z;
//    if(minZ >= maxZ){
//        maxZ = minZ + 1;
//        maxZDoubleSpinBox->setValue(maxZ);
//    }
//}
//
//void CuboidDialog::changeMaxX(double _x){
//    maxX = _x;
//    if(maxX <= minX){
//        minX = maxX - 1;
//        minXDoubleSpinBox->setValue(minX);
//    }
//}
//
//void CuboidDialog::changeMaxY(double _y){
//    maxY = _y;
//    if(maxY <= minY){
//        minY = maxY - 1;
//        minYDoubleSpinBox->setValue(minY);
//    }
//}
//
//void CuboidDialog::changeMaxZ(double _z){
//    maxZ = _z;
//    if(maxZ <= minZ){
//        minZ = maxZ - 1;
//        minZDoubleSpinBox->setValue(minZ);
//    }
//}
